package C9_5_ReplaceNestedConditionalWithGuardClauses;


/**
 * @author xugang1
 * @updated 2022/7/25
 * @description 以卫语句取代嵌套条件表达式
 */

public class ReplaceNestedConditionalWithGuardClauses {

    /**
     * 卫语句：如果某个条件极其罕见，就应该单独检查该条件，并在该条件为真时立刻从函数中返回。这样的单独检查常常被称为“卫语句”
     *
     *
     * 做法：
     * 1.对于每个检查，放进一个卫语句
     */

    /**
     * 重构前
     */
    public double getPayAmount() {
        double result;
        if (_isDead) result = deadAmount();//嵌套
        else {
            if (_isSeparated) result = separatedAmount();
            else {
                if (_isRetried) result = retriedAmount();
                else result = normalPayAmount();
            }
        }
        return result;
    }


    /**
     * 重构第一步
     */
    public double getPayAmount() {
        double result;
        if (_isDead) return deadAmount();
        if (_isSeparated) result = separatedAmount();
        else {
            if (_isRetried) result = retriedAmount();
            else result = normalPayAmount();
        }
        return result;
    }

    /**
     * 重构第二步
     */
    public double getPayAmount() {
        double result;
        if (_isDead) return deadAmount();
        if (_isSeparated) return separatedAmount();
        if (_isRetried) result = retriedAmount();
        else result = normalPayAmount();
        return result;
    }

    /**
     * 重构第三步
     */
    public double getPayAmount() {
        double result;
        if (_isDead) return deadAmount();
        if (_isSeparated) return separatedAmount();
        if (_isRetried) return retriedAmount();
        else result = normalPayAmount();
        return result;
    }

    /**
     * 重构第四步
     * 完成
     */
    public double getPayAmount() {
        double result;
        if (_isDead) return deadAmount();
        if (_isSeparated) return separatedAmount();
        if (_isRetried) return retriedAmount();
        return normalPayAmount();
    }


    ==================================================================

    /**
     *
     *  将条件反转
     *
     */


    /**
     * 重构前
     */

    public double getAdjustedCapital() {
        double result = 0.0;
        if (_capital > 0.0) {
            if (_intRate > 0.0 && _duration > 0.0) {
                result = (_income / _duration) * ADJ_FACTOR;
            }
        }
        return result;
    }

    /**
     * 重构第一步
     */
    public double getAdjustedCapital() {
        double result = 0.0;
        if (_capital <= 0.0) return result;
        if (_intRate > 0.0 && _duration > 0.0) {
            result = (_income / _duration) * ADJ_FACTOR;
        }
        return result;
    }

    /**
     * 重构第二步
     */
    public double getAdjustedCapital() {
        double result = 0.0;
        if (_capital <= 0.0) return result
        if (!(_intRate > 0.0 && _duration > 0.0)) return result;
        result = (_income / _duration) * ADJ_FACTOR;
        return result;
    }

    /**
     * 重构第三步
     */
    public double getAdjustedCapital() {
        double result = 0.0;
        if (_capital <= 0.0) return result
        if (_intRate <= 0.0 || _duration <= 0.0) return result;
        result = (_income / _duration) * ADJ_FACTOR;
        return result;
    }
    /**
     * 重构第四步
     * 完成
     */
    public double getAdjustedCapital() {
        double result = 0.0;
        if (_capital <= 0.0) return result
        if (_intRate <= 0.0 || _duration <= 0.0) return result;
        return  (_income / _duration) * ADJ_FACTOR;
    }
}